iOS property 属性关键字

  • atomic 默认关键字,表示属性是线程同步的,多线程访问时保证正确性。在移动端一般不使用,性能差。
  • nonatomic 非线程同步。
  • readwrite 默认关键字,可读可写。
  • readonly 表示属性只能读。有 getter 没有 setter。
  • writeonly 只能写不能读。
  • copy 拷贝一个新的对象,新对象的引用计数+1,原对象不用。默认会在 setter 中调用 copy 方法执行。不能对可变类型使用。
  • retain 对象的引用计数+1,arc 下不使用,用 strong 代替。
  • strong 强引用,引用计数+1,释放时-1,引用计数为0时释放对象
  • assign weak 弱引用,不希望持有对象时使用,如代理,UI 控件。对象释放时,weak 会自动变为 nil,assign 不会。
  • nonnull, nullable, null_resettable 与 swift 中的?和!类似。nonnull 表示属性不可为 nil,必须有值。nullable 表示可以为 nil。null_resettable 表示 setter 是 nullable, getter 是 nonnull。